package com.qdsgjsfk.vision.ui;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.widget.ImageView;

import com.qdsgjsfk.vision.R;
import com.rest.business.RestProxy;
import com.rest.response.QRCodeResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.util.NetUtil;

/**
 * Created by HP on 2019/9/3.
 */

public class InviteQRCodeActivity extends BaseActivity {

    @BindView(R.id.img_qr)
    ImageView img_qr;
    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        getQRCode();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_invite_qr_code;
    }

    private void getQRCode(){
        RestProxy.getInstance().getQRCode(getIntent().getStringExtra("taskId"), new Observer<QRCodeResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(QRCodeResponse qrCodeResponse) {
                byte[] decodedString = Base64.decode(qrCodeResponse.data.split(",")[1], Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                img_qr.setImageBitmap(decodedByte);
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                NetUtil.onError( e, mContext);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.refresh)
    void refresh(){
        getQRCode();
    }
}
